package com.brite.android.game.action;

import java.util.ArrayList;

import org.anddev.andengine.entity.sprite.AnimatedSprite;

public class ItemManager {
	int mTotal;
	ArrayList<Items> arrListItemsPrepare;
	ArrayList<Items> arrListItemsAvaiable;
	Region mRegion;
//	TimeCounter mTimeCounter;
	
	public ItemManager(ArrayList<Items> listItemsPrepare, int pTotal, Region pRegion) {
		mTotal = pTotal;
		mRegion = pRegion;
		arrListItemsPrepare = new ArrayList<Items>();
		arrListItemsAvaiable = new ArrayList<Items>();
		for (int i = 0; i < listItemsPrepare.size(); i++) {
			arrListItemsPrepare.add(listItemsPrepare.get(i));
		}
	}
	
//	public ItemManager(ArrayList<Items> listItemsPrepare, int pTotal, Region pRegion, float pWaitingTime) {
//		mTotal = pTotal;
//		mRegion = pRegion;
//		arrListItemsAvaiable = new ArrayList<Items>();
//		for (int i = 0; i < listItemsPrepare.size(); i++) {
//			arrListItemsPrepare.add(listItemsPrepare.get(i));
//		}
//		mTimeCounter = new TimeCounter(pWaitingTime);
//	}
	
//	public ItemManager(Items pItem, int pTotal, Region pRegion, float pWaitingTime) {
//		mItem = pItem;
//		mTotal = pTotal;
//		mRegion = pRegion;
//		arrListItemsAvaiable = new ArrayList<Items>();
//		mTimeCounter = new TimeCounter(pWaitingTime);
//	}
	
	public boolean isNeedMore() {
		if (arrListItemsAvaiable.size()<mTotal) {
			return true;
		}
		return false;
	}
	
//	public Items newItem(float pX, float pY) {
//		Items item = new Items(mItem);
//		item.mInactiveSprite.setPosition(pX, pY);
//		
//		arrListItemsAvaiable.add(item);
//		
//		return item;
//	}
	public Items newItem(Cordinate cordinate, int type) {
		for (int i = 0; i < arrListItemsPrepare.size(); i++) {
			if (type == arrListItemsPrepare.get(i).typeItem) {
				Items item = new Items(arrListItemsPrepare.get(i));
				item.mInactiveSprite.setPosition(cordinate.x, cordinate.y);
				arrListItemsAvaiable.add(item);
				return item;
			}
		}
		return null;
	}
	
	public Items getItemType(int type) {
		for (int i = 0; i < arrListItemsPrepare.size(); i++) {
			if (type == arrListItemsPrepare.get(i).typeItem) {
				Items item = new Items(arrListItemsPrepare.get(i));
				return item;
			}
		}
		return null;
	}
	
	public Items newItem(Items item) {
		arrListItemsAvaiable.add(item);
		return item;
	}
	
	public Items newItem(int type) {
		for (int i = 0; i < arrListItemsPrepare.size(); i++) {
			if (type == arrListItemsPrepare.get(i).typeItem) {
				Items item = new Items(arrListItemsPrepare.get(i));
				arrListItemsAvaiable.add(item);
				return item;
			}
		}
		return null;
	}
	
	public void removeItem(Items item) {
		arrListItemsAvaiable.remove(item);
	}
	
	public Items getCollisionItem(Creature p, boolean isActive) {
		for (int i=0; i<arrListItemsAvaiable.size(); i++) {
			Items item = arrListItemsAvaiable.get(i);
			if (item.mIsActive==isActive) {
				AnimatedSprite itemUsing = item.getCurrentSprite();
				if (isCollides(itemUsing, p)) {
					return item;
				}
			}
		}
		return null;
	}
	
	public Items checkPlayerCollision(Creature p) {
		for (int i=0; i<arrListItemsAvaiable.size(); i++) {
			Items item = arrListItemsAvaiable.get(i);
			if (item.mIsActive==false) {
				if (item.mInactiveSprite.collidesWith(p)) {
					return item;
				}
			}
		}
		return null;
	}
	
	public Items checkActiveCollision(Creature p) {
		for (int i=0; i<arrListItemsAvaiable.size(); i++) {
			Items item = arrListItemsAvaiable.get(i);
			if (item.mIsActive) {
				if (item.mActiveSprite.collidesWith(p)) {
					return item;
				}
			}
		}
		return null;
	}
	
//	private boolean isCollides(AnimatedSprite item, Creature obj){
//		float x0 = item.getX() + item.getWidth()/4;
//		float y0 = item.getY() + item.getHeight()/2;
//		float w0 = item.getWidth()/2; 
//		float h0 = item.getHeight()/2;
//		
//		float x1 = obj.getX();
//		float y1 = obj.getY();
//		float w1 = obj.getWidth();
//		float h1 = obj.getHeight();
//		
//		if (x0 <= x1  && x1 <= (x0 + w0)) {
//			if (y0 <= y1 && y1 <= (y0 + h0)){
//				return true;
//			}
//			if (y0 <= (y1 + h1) && (y1 + h1) <= (y0 + h0)){
//				return true;
//			}
//		}
//		if (x0 <= (x1+w1)  && (x1+w1) <= (x0 + w0)) {
//			if (y0 <= y1 && y1 <= (y0 + h0)){
//				return true;
//			}
//			if (y0 <= (y1 + h1) && (y1 + h1) <= (y0 + h0)){
//				return true;
//			}
//		}
//		
//		if (x1 <= x0  && x0 <= (x1 + w1)) {
//			if (y1 <= y0 && y0 <= (y1 + h1)){
//				return true;
//			}
//			if (y1 <= (y0 + h0) && (y0 + h0) <= (y1 + h1)){
//				return true;
//			}
//		}
//		if (x1 <= (x0+w0)  && (x0+w0) <= (x1 + w1)) {
//			if (y1 <= y0 && y0 <= (y1 + h1)){
//				return true;
//			}
//			if (y1 <= (y0 + h0) && (y0 + h0) <= (y1 + h1)){
//				return true;
//			}
//		}
//		return false;
//	}
	private boolean isCollides(AnimatedSprite item, Creature obj){
//		float x0 = item.getX() + item.getWidthScaled()/4;
//		float y0 = item.getY() + item.getHeightScaled()/2;
//		float w0 = item.getWidthScaled()/2; 
//		float h0 = item.getHeightScaled()/2;
		
		float x0 = item.getX() + item.getWidthScaled()/4;
		float y0 = item.getY() + item.getHeightScaled()/2;
		float w0 = item.getWidthScaled()/2; 
		float h0 = item.getHeightScaled()/2;
		
		float x1 = obj.getX();
		float y1 = obj.getY();
		float w1 = obj.getWidthScaled();
		float h1 = obj.getHeightScaled();
		
		if (x0 <= x1  && x1 <= (x0 + w0)) {
			if (y0 <= y1 && y1 <= (y0 + h0)){
				return true;
			}
			if (y0 <= (y1 + h1) && (y1 + h1) <= (y0 + h0)){
				return true;
			}
		}
		if (x0 <= (x1+w1)  && (x1+w1) <= (x0 + w0)) {
			if (y0 <= y1 && y1 <= (y0 + h0)){
				return true;
			}
			if (y0 <= (y1 + h1) && (y1 + h1) <= (y0 + h0)){
				return true;
			}
		}
		
		if (x1 <= x0  && x0 <= (x1 + w1)) {
			if (y1 <= y0 && y0 <= (y1 + h1)){
				return true;
			}
			if (y1 <= (y0 + h0) && (y0 + h0) <= (y1 + h1)){
				return true;
			}
		}
		if (x1 <= (x0+w0)  && (x0+w0) <= (x1 + w1)) {
			if (y1 <= y0 && y0 <= (y1 + h1)){
				return true;
			}
			if (y1 <= (y0 + h0) && (y0 + h0) <= (y1 + h1)){
				return true;
			}
		}
		return false;
	}
}
